<script setup>
import { computed, ref, watch } from 'vue'
import { useCartStore } from '@/stores'
import Product from '@/components/Product.vue'

const cart = useCartStore()
const props = defineProps(['shopId'])
const checkedCartListCount = ref(0)
const countPrice = ref(0)
const showProductList = ref(false)
console.log(cart.cartList)
const ShopCartList = computed(() => cart.cartList.get(props.shopId) || new Map())
console.log([...ShopCartList.value])
const ShopCartListVal = computed(() => {
  return [...ShopCartList.value]
})
console.log(ShopCartListVal.value)
watch(
  () => cart.cartList.get(props.shopId),
  () => {
    const list = cart.cartList.get(props.shopId) || new Map()
    checkedCartListCount.value = [...list.values()].reduce((all, next) => {
      if (next.number) all += 1
      return all
    }, 0)
    countPrice.value = [...list.values()].reduce((all, next) => {
      if (next.number) all += next.price * next.number
      return all
    }, 0)
  },
  {
    deep: true,
    immediate: true
  }
)
const allChecked = computed({
  get: () => {
    return [...ShopCartList.value].every((item) => {
      console.log(item[1])
      if (item[1].number) return item[1].checked
      return true
    })
  },
  set: (value) => {
    ;[...ShopCartList.value].forEach((item) => (item[1].checked = value))
  }
})
const clearCart = () => {
  cart.clearCart(props.shopId)
}
</script>
<template>
  <div class="main-cart-list">
    <div class="cart-view" @click="showProductList = !showProductList">
      <div class="cart-view-icon">
        <div class="cart_check_icon" @click="showProductList = !showProductList">
          <img src="http://www.dell-lee.com/imgs/vue3/basket.png" alt="" />
          <span v-if="checkedCartListCount > 0" class="tag"> {{ checkedCartListCount || 0 }}</span>
        </div>
      </div>
      <div class="cart-view-countPrice">
        {{ countPrice ? ' 总计:￥' + countPrice.toFixed(2) : '您还未下单呢！马上去下单' }}
      </div>
      <div
        class="cart-view-totalPrice"
        @click="$router.push({ path: '/orderConfirmation', query: { shopId: props.shopId } })"
      >
        结算
      </div>
    </div>
    <div class="cart-list" v-show="showProductList" @click.self="showProductList = false">
      <div class="cart-list-main">
        <div class="cart-list-main-header">
          <div class="checkbox">
            <input type="checkbox" class="circle-checkbox" v-model="allChecked" name="" id="" />
            &nbsp;全选
          </div>
          <div class="cart_product_header_clear" @click="clearCart">清空购物车</div>
        </div>
        <div class="cart-list-main-info">
          <div v-if="ShopCartListVal.length != 0">
            <div
              class="cart-list-main-info-item"
              v-for="item in ShopCartList"
              :key="item[1].id"
              v-show="item[1].number"
            >
              <div class="checkbox">
                <input type="checkbox" class="circle-checkbox" v-model="item[1].checked" />
              </div>
              <div class="content">
                <Product :item="item[1]" :shopId="props.shopId" :isMin="false" />
              </div>
            </div>
          </div>
          <div v-else class="cart-list-main-info-empty">购物车是空的</div>
        </div>
      </div>
    </div>
  </div>
</template>
<style scoped lang='scss'>
@import '@/style/viriables.scss';
$height: 0.7rem;
.cart-view {
  width: 100%;
  height: $height;
  position: fixed;
  bottom: 0;
  left: 0;
  display: flex;
  &-icon {
    width: 0.84rem;
    height: $height;
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    img {
      width: 0.28rem;
      height: 0.26rem;
    }
    & .tag {
      display: inline-block;
      width: 0.15rem;
      height: 0.15rem;
      border-radius: 50%;
      background-color: $hightlight-fontColor;
      color: $bgColor;
      font-size: 0.1rem;
      text-align: center;
      position: absolute;
      top: 0.05rem;
      right: 0.18rem;
    }
  }
  &-countPrice {
    height: $height;
    font-size: 15px;
    line-height: $height;
    color: $hightlight-fontColor;
  }
  &-totalPrice {
    height: $height;
    line-height: $height;
    width: 1.1rem;
    text-align: center;
    color: white;
    font-size: 15px;
    background-color: $btn-bgColor;
    position: absolute;
    right: 0px;
  }
}
.cart-list {
  width: 100vw;
  height: 100vh;
  position: fixed;
  bottom: $height;
  left: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 9999;
  display: flex;
  flex-direction: column-reverse;
  align-items: center;

  &-main {
    width: 100vw;
    background-color: #fff;
    .checkbox {
      display: flex;
      align-items: center;
      margin-left: 0.18rem;
      .circle-checkbox:checked {
        background: #1673ff;
      }

      /* 设置复选框样式 */
      .circle-checkbox {
        width: 0.2rem;
        height: 0.2rem;
        background-color: #ffffff;
        border: solid 1px $light-fontColor;
        -webkit-border-radius: 50%;
        border-radius: 50%;
        font-size: 0.8rem;
        margin: 0;
        padding: 0;
        position: relative;
        display: inline-block;
        vertical-align: top;
        cursor: default;
        -webkit-appearance: none; // 去掉默认的复选框样式
        -webkit-user-select: none; // 去掉默认的选中样式
        user-select: none;
      }

      /* 设置伪类，即显现的对勾样式 对勾样式:    */
      .circle-checkbox:checked::after {
        content: '';
        top: 5px;
        left: 4px;
        position: absolute;
        background: transparent;
        border: #fff solid 2px;
        border-top: none;
        border-right: none;
        height: 3px;
        width: 8px;
        -moz-transform: rotate(-45deg); // 火狐
        -ms-transform: rotate(-45deg); // IE9
        -webkit-transform: rotate(-45deg); // Safari
        transform: rotate(-45deg);
      }
    }
    &-info-item {
      display: flex;
    }
    &-header {
      width: 100%;
      height: 50px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 10px;
      font-size: 15px;
      font-weight: 700;
    }
    &-info {
      &-empty {
        background-color: $search-fontColor;
        text-align: center;
        font-weight: 800;
        font-size: 20px;
        line-height: 0.49rem;
        color: $content-bgColor;
      }
    }
  }
}
</style>